package android07.newtop.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TableLayout;
import android.widget.Toast;

import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.data.BeamDataActivity;

import android07.newtop.R;
import android07.newtop.model.bean.MainBean;
import android07.newtop.presenter.MainActivityPresenter;
import android07.newtop.utils.MyUtil;

/**
 * 首页
 * 主要功能
 * 多页面展示新闻
 *
 * 主要涉及：
 * 1、侧滑DrawerLayout+Toolbar
 * 2、viewpager
 * 3、tablayout布局 导航栏
 * 4、beam开发框架集成recycleview列表 下拉刷新，上啦加载等等一些列的功能 ，无比强大
 *
 * Created by hy on 2016/10/4
 */
@RequiresPresenter(MainActivityPresenter.class)
public class MainActivity extends BeamDataActivity<MainActivityPresenter, MainBean> implements NavigationView.OnNavigationItemSelectedListener {
    private static final String TAG = MainActivity.class.getSimpleName();
    private NavigationView navview;
    private DrawerLayout drawerlayout;
    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //透明状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }


        setContentView(R.layout.activity_main);
        //findview
        initView();

        //使策划蓝的 icon 的颜色恢复本色
        navview.setItemIconTintList(null);
        //设置toolbar
        setSupportActionBar(toolbar);
//        AppBarLayout.LayoutParams param = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
//        param.setMargins(0, 0, 0, 0);
//        toolbar.setLayoutParams(param);

        blur();
        //设置侧滑
        setDrawlayout();


    }

    private void initView() {
        this.drawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        this.navview = (NavigationView) findViewById(R.id.nav_view);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        //       tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }


    /**
     * 给其他类获得viewpage的方法
     *
     * @return
     */
    public ViewPager getViewPager() {
        return viewPager;
    }


    /**
     * 给其他类获得TableLayout的方法
     *
     * @return
     */
    public TabLayout getTabLayout() {
        return tabLayout;
    }




    /**
     * 初始化策划菜单
     */
    private void setDrawlayout() {

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerlayout, toolbar, R.string.opencehua, R.string.closecehua);
        drawerlayout.addDrawerListener(actionBarDrawerToggle);
        // 抽屉的同步状态指示器链接DrawerLayout的指示
        actionBarDrawerToggle.syncState();
        navview.setNavigationItemSelectedListener(this);

    }

    @Override
    public void setData(MainBean data) {
        super.setData(data);


    }


    /**
     * 当侧滑栏被点击的时候 ，执行的方法
     *
     * @param item
     * @return
     */
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.nav_newtop:

                Toast.makeText(MainActivity.this, "新闻", Toast.LENGTH_SHORT).show();

                break;

            case R.id.nav_newtop1:
                Toast.makeText(MainActivity.this, "美图", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(MainActivity.this,PictureActivity.class));
                break;
            case R.id.nav_newtop2:
//                Toast.makeText(MainActivity.this, "天气", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(MainActivity.this,WeatherActivity.class));
                break;
            case R.id.nav_newtop3:
                Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_SHORT).show();
                break;


        }


        Log.d(TAG, "onNavigationItemSelected:itemId= " + itemId);
        drawerlayout.closeDrawers();
        return false;
    }

    private void blur() {
        new AsyncTask<Void, Void, Void>() {
            Drawable wallPaperBlurDrawable;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
//                mDensity = getResources().getDisplayMetrics().density;

                wallPaperBlurDrawable = MyUtil.getWallPaperBlurDrawable(MainActivity.this);
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                drawerlayout.setBackground(wallPaperBlurDrawable);
                super.onPostExecute(aVoid);
            }
        }.execute();
    }

}
